package visaoInterface;

import javax.swing.JOptionPane;
import dao.DaoPessoa;
import dao.DaoFuncionario;
import dao.DaoEndereco;
import javax.swing.JOptionPane;
import modelo.dominio.*;

public class ChamaFuncionario extends javax.swing.JDialog {

    /** Creates new form ChamaFuncionário */
    public ChamaFuncionario(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLFNome = new javax.swing.JLabel();
        jLFCpf = new javax.swing.JLabel();
        jLFTelefone = new javax.swing.JLabel();
        jLFRua = new javax.swing.JLabel();
        jLFNumero = new javax.swing.JLabel();
        jTFFNome = new javax.swing.JTextField();
        jTFFRua = new javax.swing.JTextField();
        jLFBairro = new javax.swing.JLabel();
        jTFFBairro = new javax.swing.JTextField();
        jLFEndereco = new javax.swing.JLabel();
        jLCargo = new javax.swing.JLabel();
        jTFCargo = new javax.swing.JTextField();
        jBFCancelar = new javax.swing.JButton();
        jBFSalvar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jBFLimpar = new javax.swing.JButton();
        jTFFCpf = new javax.swing.JFormattedTextField();
        jTFFTelefone = new javax.swing.JFormattedTextField();
        jTFFNumero = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Funcionário"));

        jLFNome.setText("Nome*");

        jLFCpf.setText("CPF*");

        jLFTelefone.setText("Telefone");

        jLFRua.setText("Rua");

        jLFNumero.setText("Número");

        jTFFNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFFNomeActionPerformed(evt);
            }
        });

        jLFBairro.setText("Bairro");

        jLFEndereco.setText("Endereço:");

        jLCargo.setText("Cargo*");

        jTFCargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFCargoActionPerformed(evt);
            }
        });

        jBFCancelar.setText("Cancelar");
        jBFCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBFCancelarActionPerformed(evt);
            }
        });

        jBFSalvar.setText("Salvar");
        jBFSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBFSalvarActionPerformed(evt);
            }
        });

        jLabel1.setText("* Campos Obrigatórios");

        jBFLimpar.setText("Limpar");
        jBFLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBFLimparActionPerformed(evt);
            }
        });

        try {
            jTFFCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jTFFTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLFEndereco)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLFRua)
                                .addGap(27, 27, 27))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLFBairro)
                                .addGap(18, 18, 18)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTFFBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTFFRua, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                                    .addComponent(jTFCargo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addComponent(jLFNumero)
                                .addGap(18, 18, 18)
                                .addComponent(jTFFNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jLCargo)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                        .addComponent(jBFLimpar)
                        .addGap(18, 18, 18)
                        .addComponent(jBFSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(jBFCancelar))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLFNome)
                            .addComponent(jLFCpf))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jTFFCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(33, 33, 33)
                                .addComponent(jLFTelefone)
                                .addGap(18, 18, 18)
                                .addComponent(jTFFTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
                            .addComponent(jTFFNome, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFFNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLFNome))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLFCpf)
                    .addComponent(jTFFCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFFTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLFTelefone))
                .addGap(18, 18, 18)
                .addComponent(jLFEndereco)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLFRua)
                    .addComponent(jTFFRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFFNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLFNumero))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFFBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLFBairro))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLCargo)
                    .addComponent(jTFCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jBFCancelar)
                        .addComponent(jBFSalvar)
                        .addComponent(jBFLimpar))
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTFFNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFFNomeActionPerformed

    }//GEN-LAST:event_jTFFNomeActionPerformed

    private void jTFCargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFCargoActionPerformed

        // TODO add your handling code here:}//GEN-LAST:event_jTFCargoActionPerformed
    }
        private void jBFCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBFCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBFCancelarActionPerformed

    private void jBFLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBFLimparActionPerformed
        //limpa os dados das caixas de texto.
        jTFFNome.setText("");
        jTFFCpf.setText("");
        jTFFTelefone.setText("");
        jTFFRua.setText("");
        jTFFNumero.setText("");
        jTFFBairro.setText("");
        jTFCargo.setText("");
    }//GEN-LAST:event_jBFLimparActionPerformed

    private void jBFSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBFSalvarActionPerformed
        //Mostra o dado dos funcionarios na tabela.
        int cod;
        if(jTFFNome.getText().equals("") || jTFFCpf.getText().equals("") || jTFCargo.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Preencher os campos obrigatórios.");
        }else{
            
        String cpf = jTFFCpf.getText();        
        if (cpf.length() > 11){
        cpf = cpf.replaceAll("\\.", "");
        cpf = cpf.replaceAll("-","");
        }
        Pessoa p = new Pessoa();
        if (p.verificaCpf(cpf) && p.validaCpf(cpf)){ 
        Funcionario f = new Funcionario();
        Endereco e = new Endereco();
        DaoFuncionario df = new DaoFuncionario();
        DaoPessoa dp = new DaoPessoa();
        DaoEndereco de = new DaoEndereco();
        p.setNome(jTFFNome.getText());
        p.setCpf(jTFFCpf.getText());
        p.setTelefone(jTFFTelefone.getText());
        f.setCpf(jTFFCpf.getText());
        e.setRua(jTFFRua.getText());
        if (jTFFNumero.getText().equals("")){
            e.setNumero(0);
        }else{
            e.setNumero(Integer.parseInt(jTFFNumero.getText()));
        }
        e.setBairro(jTFFBairro.getText());
        de.insere(e);
        df.insere(f);
        cod = de.retornaCodUltimoRegistro();
        int codFuncionario = df.retornaCodUltimoRegistro();
        dp.insereFuncionario(p,cod,codFuncionario);
        
        jTFFNome.setText("");
        jTFFCpf.setText("");
        jTFFTelefone.setText("");
        jTFFRua.setText("");
        jTFFNumero.setText("");
        jTFFBairro.setText("");
        jTFCargo.setText("");
        JOptionPane.showMessageDialog(null, "O Funcionario foi cadastrado com sucesso.");
        }else{
        JOptionPane.showMessageDialog(null, "CPF não corresponde a um CPF valido.");
        jTFFCpf.setText("");    
        }
        }
    }//GEN-LAST:event_jBFSalvarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChamaFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChamaFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChamaFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChamaFuncionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChamaFuncionario dialog = new ChamaFuncionario(new javax.swing.JDialog(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBFCancelar;
    private javax.swing.JButton jBFLimpar;
    private javax.swing.JButton jBFSalvar;
    private javax.swing.JLabel jLCargo;
    private javax.swing.JLabel jLFBairro;
    private javax.swing.JLabel jLFCpf;
    private javax.swing.JLabel jLFEndereco;
    private javax.swing.JLabel jLFNome;
    private javax.swing.JLabel jLFNumero;
    private javax.swing.JLabel jLFRua;
    private javax.swing.JLabel jLFTelefone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTFCargo;
    private javax.swing.JTextField jTFFBairro;
    private javax.swing.JFormattedTextField jTFFCpf;
    private javax.swing.JTextField jTFFNome;
    private javax.swing.JTextField jTFFNumero;
    private javax.swing.JTextField jTFFRua;
    private javax.swing.JFormattedTextField jTFFTelefone;
    // End of variables declaration//GEN-END:variables
}
